package com.hsy.crm.web.service.base;

import com.hsy.crm.web.domain.entity.Menu;
import com.hsy.crm.web.utils.ExpiryMap;

import java.util.List;
import java.util.Map;

public class CacheService {

    public static final String MENUS_KEY = "menus";
    public static final String PRODUCT_KEY = "product";
    public static final String VENDOR_KEY = "vendor";
    public static final String CUSTOMER_KEY = "customer";

    // ----------------------------------customer-----------------------------------------

    public static void putCustomerIds(String customerId,List<Long> productIds){
        ExpiryMap.put(CUSTOMER_KEY+customerId,productIds);
    }

    public static void removeCustomerId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(CUSTOMER_KEY+customerId);
        productIds.remove(productId);
    }

    public static void putCustomerId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(CUSTOMER_KEY+customerId);
        productIds.add(productId);
    }

    public static boolean hasCustomerId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(CUSTOMER_KEY+customerId);
        for( Long id : productIds ){
            if( id.longValue() == productId.longValue() ){
                return true;
            }
        }
        return false;
    }

    // ----------------------------------vendor-----------------------------------------

    public static void putVendorIds(String customerId,List<Long> productIds){
        ExpiryMap.put(VENDOR_KEY+customerId,productIds);
    }

    public static void removeVendorId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(VENDOR_KEY+customerId);
        productIds.remove(productId);
    }

    public static void putVendorId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(VENDOR_KEY+customerId);
        productIds.add(productId);
    }

    public static boolean hasVendorId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(VENDOR_KEY+customerId);
        for( Long id : productIds ){
            if( id.longValue() == productId.longValue() ){
                return true;
            }
        }
        return false;
    }

    // ----------------------------------product-----------------------------------------

    public static void putProductIds(String customerId,List<Long> productIds){
        ExpiryMap.put(PRODUCT_KEY+customerId,productIds);
    }

    public static void removeProductId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(PRODUCT_KEY+customerId);
        productIds.remove(productId);
    }

    public static void putProductId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(PRODUCT_KEY+customerId);
        productIds.add(productId);
    }

    public static boolean hasProductId(String customerId,Long productId){
        List<Long> productIds = (List<Long>) ExpiryMap.get(PRODUCT_KEY+customerId);
        for( Long id : productIds ){
            if( id.longValue() == productId.longValue() ){
                return true;
            }
        }
        return false;
    }

    // ---------------------------------menu------------------------------------------

    public static void putMenus(Map<Long,Menu> menus){
        ExpiryMap.put(MENUS_KEY,menus);
    }

    public static Map<Long,Menu> getMenus(){
        return (Map<Long,Menu>) ExpiryMap.get(MENUS_KEY);
    }

}
